package com.qf.homework.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/user")
public class UserController {
   private Map<String,String> map=new HashMap<>();
    @RequestMapping("/test")
    public void test5(String username, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //解决中文乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        //获取前端向后端发送的数据
        String value = req.getParameter("value");

        String  result=null;
        boolean b = map.containsKey(value);
        if (b){
            result="2";
        }else {
            result="1";
            map.put(value,"aaaa");
        }
        resp.getWriter().print(result);
        }


    @RequestMapping("/test1")
    public void test1(String username, String kapchar, HttpSession session,HttpServletResponse resp,HttpServletRequest req) throws IOException {

        //1. 校验验证码是否正确，从session中获取验证码和请求参数中的验证码进行比对
        // final String sessionCapchar = (String) session.getAttribute("captcha");
        //括号中的"captcha"去web.xml中     <param-value>captcha</param-value> 中 拿
        String sessionCapchar = (String) session.getAttribute("captcha");
        String s = sessionCapchar.toLowerCase();
        final HashMap<String, String> map = new HashMap<>();
        //获取前端向后端发送的数据
        String value = req.getParameter("value");

        String s1 = value.toLowerCase();
        String  result=null;
        if (s.equals(s1)){
            result="1";

        }else {
            result="2";
        }
        resp.getWriter().print(result);


    }


    @RequestMapping("/login")
    public Map<String,String> login( String kapchar, HttpSession session) throws IOException {

        //1. 校验验证码是否正确，从session中获取验证码和请求参数中的验证码进行比对
        // final String sessionCapchar = (String) session.getAttribute("captcha");
        //括号中的"captcha"去web.xml中     <param-value>captcha</param-value> 中 拿
        String sessionCapchar = (String) session.getAttribute("captcha");
        String s = sessionCapchar.toLowerCase();
        String s1 = kapchar.toLowerCase();
        final HashMap<String, String> map = new HashMap<>();
        String  result=null;
        if (s.equals(s1)){
            // 验证码校验同通过， 继续查询数据，查username的用户，然后比对密码
            map.put("code","1");
            map.put("msg","注册成功过了");
        }

         return map;

    }
}
